package net.vinote.sosa.web.interceptor;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.springframework.web.servlet.handler.HandlerInterceptorAdapter;

import com.alibaba.fastjson.JSONObject;

/**
 * 设置Sosa默认拦截器
 * 
 * @author Seer
 * @version SosaDefaultInterceptor.java, v 0.1 2015年10月10日 下午3:01:56 Seer Exp.
 */
public class SosaDefaultInterceptor extends HandlerInterceptorAdapter {

	@Override
	public void afterCompletion(HttpServletRequest request,
			HttpServletResponse response, Object handler, Exception ex)
			throws Exception {
		if (ex != null && request.getServletPath().endsWith(".json")) {
			JSONObject json = new JSONObject();
			json.put("success", false);
			json.put("message", ex.getMessage());
			response.setContentType("application/json");
			response.getOutputStream().write(json.toString().getBytes());
			response.getOutputStream().flush();
		}
	}

}
